package dao;

import entity.Song;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;

/**
 *
 * @author Javier
 */
public class SongDao {
    private EntityManagerFactory emf = null;
    private EntityManager em = null;
    private EntityTransaction et = null;
    
    
    public boolean persistSong(Song song){    
        boolean result=false;
        try{
        emf = Persistence.createEntityManagerFactory("GRLUPUPU");
        em = emf.createEntityManager();
        et = em.getTransaction();
        et.begin();
        
        song = em.merge(song);
        em.flush();
        em.persist(song);
        
        et.commit(); 
        result=true;
        }catch(Exception e){
            if (em != null && et != null){
                et.rollback();
            }
        }finally{
            if (em != null){
                em.clear();
                em.close();
            }
            return result;
        }
    }
    public boolean deleteSong(Song song) {
        boolean result = false;
        try {
            emf = Persistence.createEntityManagerFactory("GRLUPUPU");
            em = emf.createEntityManager();
            et = em.getTransaction();
            et.begin();        
            em.remove(song);
            em.getTransaction().commit();
            result = true;
        } catch (Exception e) {
            if (em != null && et != null){
                et.rollback();
            }
        } finally {
            if (em != null){
                em.clear();
                em.close();
            }
            return result;
        }
    }
    
    
    
}
